<?php

	namespace Phptricks\system\library\hooks;

	/**
	 *  phptricks Framework 
	 *  link http://phptricks.net/Framework
	 *  date start 2014
	 * Description of hooks
	 * @author Abd Elfttah Ahmed <phptricks.net@gmail.com>
	 */
	use Phptricks\system\core\library\library;
	use Phptricks\system\library\PhptricksExceptions\PhptricksExceptions as Exc;
	use Phptricks\system\library\loaders\loader as loader;
	use Phptricks\system\library\request_variables\request_variables as request;

	class hooks implements library{

		public $_loader = null;
		public $active = false;
		public $hooks = null;
		public $hooks_run = null;
		public $_uri = null;
		public $_request = null;
		private static $_instance = null;

		public function __construct(){
			
		}

		public static function getinstance(){
			if (self::$_instance === null) {
				self::$_instance = new \Phptricks\system\library\hooks\hooks();
			}
			return self::$_instance;
		}

		public function _initialize(&$uri = '', $active = false){
			if (!isset($uri) || empty($uri)) {
				throw new Exc('Url is Not Set');
			}
			$this->_uri = &$uri;
			$this->_request = request::getinstance();
			$this->active = $active;
			$this->hooks = array(
				'after' => array(),
				'before' => array()
			);
			loader::load_dir(APPPATH . 'hooks/');
		}

		public function set_router($array = array()){
			if (isset($array) && count($array) > 0) {
				foreach ($array as $key => $val) {
					if (isset($this->_uri[$key])) {
						$this->_uri[$key] = $val;
					} else {
						$this->_uri['args'][] = $val;
					}
				}
			}
		}

		public function set($hooks_name, $function, $type = 'after'){
			switch ($type) {
				case 'after':
					$this->hooks['after'][$hooks_name] = $function;
					break;

				case 'before':
					$this->hooks['before'][$hooks_name] = $function;
					break;

				default:
					$this->hooks['before'][$hooks_name] = $function;
					break;
			}
		}

		public function _run($type = 'before'){
			foreach ($this->hooks[$type] as $key => $value) {
				if (\preg_match('/\//iUs', $key) == true) {
					$keys = \explode('/', $key);
					if ($this->_uri['class'] == $keys[0] && $this->_uri['function'] == $keys[1]) {
						if ($this->_uri['function'] != 'index') {
							if (\function_exists($value)) {
								if (isset($this->_uri['args']) && count($this->_uri['args']) > 0) {
									$value($this->_uri['args']);
								} else {
									$value($this->_uri['function']);
								}
							}
						} else {
							if (\function_exists($value)) {
								if (isset($this->_uri['args']) && count($this->_uri['args']) > 0) {
									$value($this->_uri['args']);
								} else {
									$value();
								}
							} else {
								throw new Exc('function :' . $value . ' is Not Found');
							}
						}
					}
				} else {
					if (\function_exists($value)) {
						if (isset($this->_uri['args']) && count($this->_uri['args']) > 0) {
							$value($this->_uri['args']);
						} else {
							$value();
						}
					} else {
						throw new Exc('function :' . $value . ' is Not Found');
					}
				}
			}
			return true;
		}

		public function is_sset($array_hooks){
			if (array_key_exists($array_hooks, $this->hooks['after']) || array_key_exists($array_hooks, $this->hooks['before'])) {
				return true;
			}
			return false;
		}

	}
	